<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of product_model
 *
 * @author Administrator
 */
class product_model extends CI_Model {

    //put your code here
    function __construct() {
        parent::__construct();
    }

    function getAllProducts() {

        $query = $this->db->get_where(tbl_products, array('product_is_deleted' => 0));
        return $query->result();
    }

    function getOldProducts() {


        $this->db->select('tb_products.product_id,product_name,product_company_price,product_tan_phat_price,product_date,product_name_segment,image_url');
        $this->db->from(tbl_products);
        $this->db->join(tbl_product_images, 'tb_products.product_id=tb_product_images.product_id');
        $arr = array(
            'image_is_default' => 1,
            'product_is_old' => 1,
            'product_is_display' => 1,
            'product_is_deleted' => 0,
            'image_is_delete' => 0,
            'image_display_state' => 1
        );
        $this->db->where($arr);
        $query = $this->db->get();
        $this->db->order_by('product_date', 'desc');
        return $query->result();
    }

    function getProductByID($id) {

        $query = $this->db->get_where(tbl_products, array('product_id' => $id));
        $arr = $query->result();
        $count = $query->num_rows();
        if ($count <= 0)
            return null;

        return $arr[0];
    }

    function getProductByCategory_Count($category_id) {
        $this->db->select('tb_products.product_id,product_name,product_company_price,product_tan_phat_price,product_date,product_name_segment,category_id,image_url');
        $this->db->from(tbl_products);
        $this->db->join(tbl_product_images, 'tb_products.product_id=tb_product_images.product_id');
        $arr = array(
            'image_is_default' => 1,
            'product_is_new' => 1,
            'product_is_display' => 1,
            'product_is_deleted' => 0,
            'image_is_delete' => 0,
            'image_display_state' => 1,
            'image_is_default' => 1,
            'category_id' => $category_id
        );
        $this->db->where($arr);
        $query = $this->db->get();
        return $query->num_rows();
    }

    function getProductByCategory($category_id, $page = 0, $pagesize = PAGE_SIZE) {


        $this->db->limit($pagesize, $page);
        $this->db->select('tb_products.product_id,product_name,product_company_price,product_tan_phat_price,product_date,product_name_segment,category_id,image_url');
        $this->db->from(tbl_products);
        $this->db->join(tbl_product_images, 'tb_products.product_id=tb_product_images.product_id');
        $arr = array(
            'image_is_default' => 1,
            'product_is_new' => 1,
            'product_is_display' => 1,
            'product_is_deleted' => 0,
            'image_is_delete' => 0,
            'image_display_state' => 1,
            'image_is_default' => 1,
            'category_id' => $category_id
        );
        $this->db->where($arr);
        $query = $this->db->get();
        $this->db->order_by('product_date', 'desc');
        return $query->result();
    }

    function getOldProductByCategory($category_id) {


        $this->db->select('tb_products.product_id,product_name,product_company_price,product_tan_phat_price,product_date,product_name_segment,category_id,image_url');
        $this->db->from(tbl_products);
        $this->db->join(tbl_product_images, 'tb_products.product_id=tb_product_images.product_id');
        $arr = array(
            'image_is_default' => 1,
            'product_is_old' => 1,
            'product_is_display' => 1,
            'product_is_deleted' => 0,
            'image_is_delete' => 0,
            'image_display_state' => 1,
            'tb_product_images.image_is_default' => 1,
            'category_id' => $category_id
        );
        $this->db->where($arr);
        $query = $this->db->get();
        $this->db->order_by('product_date', 'desc');
        return $query->result();
    }
    function getFeatureProduct($limit) {

        $this->db->limit($limit);
        $this->db->select('tb_products.product_id,product_name,product_company_price,product_tan_phat_price,product_date,product_name_segment,image_url');
        $this->db->from(tbl_products);
        $this->db->join(tbl_product_images, 'tb_products.product_id=tb_product_images.product_id');
        $arr = array(
            'image_is_default' => 1,
            'product_is_new' => 1,
            'product_is_display' => 1,
            'product_is_deleted' => 0,
            'image_is_delete' => 0,
            'image_display_state' => 1
        );
        $this->db->where($arr);
        $query = $this->db->get();
        $this->db->order_by('product_viewer', 'desc');
        return $query->result();
    }

    function getNewProduct($limit) {

        $this->db->limit($limit);
        $this->db->select('tb_products.product_id,product_name,product_company_price,product_tan_phat_price,product_date,product_name_segment,image_url');
        $this->db->from(tbl_products);
        $this->db->join(tbl_product_images, 'tb_products.product_id=tb_product_images.product_id');
        $arr = array(
            'image_is_default' => 1,
            'product_is_new' => 1,
            'product_is_display' => 1,
            'product_is_deleted' => 0,
            'image_is_delete' => 0,
            'image_display_state' => 1
        );
        $this->db->where($arr);
        $query = $this->db->get();
        $this->db->order_by('product_date', 'desc');
        return $query->result();
    }

    function getAdvancedProduct($limit, $min_price,$max_price) {

        $this->db->limit($limit);
        $this->db->select('tb_products.product_id,product_name,product_company_price,product_tan_phat_price,product_date,product_name_segment,image_url');
        $this->db->from(tbl_products);
        $this->db->join(tbl_product_images, 'tb_products.product_id=tb_product_images.product_id');
        $arr = array(
            'image_is_default' => 1,
            'product_is_new' => 1,
            'product_is_display' => 1,
            'product_is_deleted' => 0,
            'image_is_delete' => 0,
            'image_display_state' => 1,
            'product_company_price <=' => $max_price,
            'product_company_price >='=>$min_price
        );

        $this->db->where($arr);

        $this->db->order_by('product_company_price', 'desc');

        $query = $this->db->get();

        return $query->result();
    }

    function getGeneralProduct($limit, $min_price,$max_price) {

        $this->db->limit($limit);
        $this->db->select('tb_products.product_id,product_name,product_company_price,product_tan_phat_price,product_date,product_name_segment,image_url');
        $this->db->from(tbl_products);
        $this->db->join(tbl_product_images, 'tb_products.product_id=tb_product_images.product_id');
        $arr = array(
            'image_is_default' => 1,
            'product_is_new' => 1,
            'product_is_display' => 1,
            'product_is_deleted' => 0,
            'image_is_delete' => 0,
            'image_display_state' => 1,
            'product_company_price <=' => $max_price,
            'product_company_price >='=>$min_price
        );

        $this->db->where($arr);

        $this->db->order_by('product_company_price', 'desc');

        $query = $this->db->get();

        return $query->result();
    }

    function search($product_name, $category_id, $price_from, $price_to) {


        $this->db->select('tb_products.product_id,product_name,product_company_price,product_tan_phat_price,product_date,product_name_segment,image_url');
        $this->db->from(tbl_products);
        $this->db->join(tbl_product_images, 'tb_products.product_id=tb_product_images.product_id');
        $arr = array(
            'image_is_default' => 1,
            'product_is_new' => 1,
            'product_is_display' => 1,
            'product_is_deleted' => 0,
            'image_is_delete' => 0,
            'image_display_state' => 1,
        );


        $this->db->where($arr);

        if (is_numeric($category_id)) {
            $this->db->where('category_id', $category_id);
        }
        if (is_numeric($price_from)) {
            $this->db->where('product_company_price >= ', $price_from);
        }
        if (is_numeric($price_to)) {
            $this->db->where('product_company_price <= ', $price_to);
        }
        $this->db->or_like('product_name_segment', $product_name);
        //$this->db->or_like('product_name_segment',$product_name);


        $this->db->order_by('product_company_price', 'desc');

        $query = $this->db->get();

        return $query->result();
    }

    function getAllProducts_Admin($page = 0, $pagesize = PAGE_SIZE) {
        $this->db->limit($pagesize, $page);
        $this->db->select('p.*,pi.image_url,c.category_name');
        $this->db->from(tbl_products . ' p');
        $this->db->join(tbl_product_images . ' pi', 'p.product_id=pi.product_id');
        $this->db->join(tbl_category . ' c', 'c.category_id=p.category_id');
        $this->db->where('p.product_is_deleted', 0);
        $this->db->where('pi.image_is_default', 1);
       
        $query = $this->db->get();


        $this->db->select('product_id');
        $this->db->from(tbl_products);
        $this->db->where('product_is_deleted', 0);
        $total = $this->db->count_all_results();



        $data = array('list' => $query->result(), 'total' => $total);
        return $data;
    }
  function getOldProducts_Admin($page = 0, $pagesize = PAGE_SIZE) {
        $this->db->limit($pagesize, $page);
        $this->db->select('p.*,pi.image_url,c.category_name');
        $this->db->from(tbl_products . ' p');
        $this->db->join(tbl_product_images . ' pi', 'p.product_id=pi.product_id');
        $this->db->join(tbl_category . ' c', 'c.category_id=p.category_id');
        $arr=array('p.product_is_deleted'=>0,'p.product_is_old'=>1,'pi.image_is_default'=>1);
        $this->db->where($arr);
        $query = $this->db->get();


        $this->db->select('product_id');
        $this->db->from(tbl_products);
        $this->db->where('product_is_deleted', 0);
        $total = $this->db->count_all_results();



        $data = array('list' => $query->result(), 'total' => $total);
        return $data;
    }
    function getNewProducts_Admin($page = 0, $pagesize = PAGE_SIZE) {
        $this->db->limit($pagesize, $page);
        $this->db->select('p.*,pi.image_url,c.category_name');
        $this->db->from(tbl_products . ' p');
        $this->db->join(tbl_product_images . ' pi', 'p.product_id=pi.product_id');
        $this->db->join(tbl_category . ' c', 'c.category_id=p.category_id');
        $this->db->where('p.product_is_deleted', 0);
        $this->db->where('p.product_is_new',1);
        $this->db->where('pi.image_is_default', 1);
        $query = $this->db->get();


        $this->db->select('product_id');
        $this->db->from(tbl_products);
        $this->db->where('product_is_deleted', 0);
        $total = $this->db->count_all_results();



        $data = array('list' => $query->result(), 'total' => $total);
        return $data;
    }
    
    function addProduct($info) {
        return $this->db->insert(tbl_products, $info);
    }

    function updateProduct($info, $id) {
        $this->db->where('product_id', $id);
        return $this->db->update(tbl_products, $info);
    }

    function deleteProduct($id) {
        $this->db->where('product_id', $id);
        return $this->db->update(tbl_products, array('product_is_deleted' => 1));
     
    }

    
    function updateViewer($id,$value){
        $this->db->where('product_id',$id);
        return $this->db->update(tbl_products,array('product_viewer'=>$value));
    }
}

?>
